import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Arrays;

public class SpamArffsLevel2 {
	static String PATH_LABEL = "../LABELS.txt";
	static byte[] label;
	static int ROWS;

	static String PATH_INPUT = "../val";
	static byte[][] input;
	static File[] listOfFiles;
	static int COLS;

	static String PATH_SPAM = "../spam/";

	public static void main(String[] args) throws Exception {
		readLabel_ROWS();
		readInputs();
		makeSpamDir();
		createSpamFiles();
	}

	static void createSpamFiles() throws Exception {
		int counter = 0;
		for (int i = 0; i < listOfFiles.length; i++) {
			for (int j = i + 1; j < listOfFiles.length; j++) {
				System.out.println(counter);
				counter++;

				// creation of file name
				String spamFileOutString = PATH_SPAM + "2-" + i + "_" + j
						+ ".arff";

				// print values of arff file
				FileWriter fw = new FileWriter(spamFileOutString);
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write("@relation r\n");
				String fileName;
				String attName;

				// i
				fileName = listOfFiles[i].getName();
				attName = fileName.substring(0, 5);
				bw.write("@attribute " + attName + " numeric\n");

				// k
				fileName = listOfFiles[j].getName();
				attName = fileName.substring(0, 5);
				bw.write("@attribute " + attName + " numeric\n");

				bw.write("@attribute label numeric\n");
				bw.write("@data\n");

				for (int r = 0; r < ROWS; r++) {
					bw.write(input[i][r] + ",");
					bw.write(input[j][r] + ",");
					bw.write(label[r] + "\n");
				}
				bw.close();
			}
		}
	}

	static void makeSpamDir() {
		File dir = new File(PATH_SPAM);
		if (!dir.exists()) {
			dir.mkdirs();
		}
	}

	static void readLabel_ROWS() throws Exception {
		File file = new File(PATH_LABEL);
		FileInputStream fis = new FileInputStream(file);
		ROWS = (int) file.length();
		label = new byte[ROWS];

		fis.read(label);
	}

	static void readInputs() throws Exception {
		File folder = new File(PATH_INPUT);
		listOfFiles = folder.listFiles();
		Arrays.sort(listOfFiles);
		COLS = listOfFiles.length;
		ROWS = (int) listOfFiles[0].length();
		input = new byte[COLS][ROWS];

		for (int c = 0; c < COLS; c++) {
			if (listOfFiles[c].isFile()) {
				FileInputStream fis = new FileInputStream(listOfFiles[c]);
				fis.read(input[c]);
			}
		}
	}

}
